package com.hopes.bizz.packet.hj212.support;

import com.hopes.bizz.packet.hj212.client.Hj212Client;
import com.hopes.bizz.packet.hj212.constant.HJ212Constant;
import com.hopes.bizz.packet.hj212.handler.Hj212DataSendHandler;
import io.netty.channel.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/3/9 20:09
 * @Description:
 */
@Slf4j
@Component
public class Hj212ClientManger {
    @Autowired
    private Hj212Client client;
    @Value("${hj212.config.real.enable}")
    private Boolean real;
    @Value("${hj212.config.min.enable}")
    private Boolean min;
    @Value("${hj212.config.hour.enable}")
    private Boolean hour;
    @Value("${hj212.config.day.enable}")
    private Boolean day;

    @Autowired
    private Hj212DataSendHandler hj212DataSendHandler;

    @SneakyThrows
//    @Scheduled(cron = "* * * * * ?")
    @Scheduled(cron = "0 0/1 * * * ?")
    public void scheduleReal(){
        if (real != null && real) {
            log.info("发送实时数据");
            Channel channel = client.createChannel();
            send(channel, HJ212Constant.COMMAND_REAL_TIME_DATA);
            channel.close();
        }
    }

    @SneakyThrows
    @Scheduled(cron = "0 0/1 * * * ?")
    public void scheduleMin(){
        if (min != null && min) {
            log.info("发送分钟数据");
            Channel channel = client.createChannel();
            send(channel, HJ212Constant.COMMAND_MINUTE_DATA);
            channel.close();
        }
    }

    @SneakyThrows
    @Scheduled(cron = "0 0 * * * ?")
    public void scheduleHour(){
        if (hour != null && hour) {
            log.info("发送小时数据");
            Channel channel = client.createChannel();
            send(channel, HJ212Constant.COMMAND_HOUR_DATA);
            channel.close();
        }
    }

    @SneakyThrows
    @Scheduled(cron = "0 0 * * * ?")
    public void scheduleDay(){
        if (day != null && day) {
            log.info("发送天数据");
            Channel channel = client.createChannel();
            send(channel, HJ212Constant.COMMAND_DAY_DATA);
            channel.close();
        }
    }

    @SneakyThrows
    private void send(Channel channel, String cn) {
        Hj212DataBuilder builder = Hj212DataBuilder.getInstance();
        // 1.根据配置文件的各个因子报警级别，构建报文内容
        List<Hj212DataAdapter> list = hj212DataSendHandler.handle(cn);
        for (Hj212DataAdapter hj212DataAdapter : list) {
            // 2.构建报文
            String hj212Msg = builder.build(hj212DataAdapter);
            if (channel.isActive()) {
                channel.writeAndFlush(hj212Msg);
                log.info("已发送数据:{}", hj212Msg);
            }
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
